import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";

var _dec, _class;

/**
 * 监听 websocket
 */
import React from 'react';
import { connect } from 'dva';
import Icons from "../../../components/Icons";
import { Bind } from 'lodash-decorators';
import { notification as Notification } from 'antd';
import uuid from 'uuid/v4';
import webSocketManager from "../../../utils/webSoket";
import notification from "../../../utils/notification";
import { getCurrentOrganizationId } from "../../../utils/utils";
import { openTab } from "../../../utils/menuTab";
import intl from "../../../utils/intl";
var DefaultListenWebSocket = (_dec = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(DefaultListenWebSocket, _React$Component);

  var _super = _createSuper(DefaultListenWebSocket);

  function DefaultListenWebSocket() {
    _classCallCheck(this, DefaultListenWebSocket);

    return _super.apply(this, arguments);
  }

  _createClass(DefaultListenWebSocket, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this = this;

      webSocketManager.initWebSocket();
      webSocketManager.addListener('cpaas-web', function (messageData) {
        var _this$props = _this.props,
            saveNotices = _this$props.saveNotices,
            count = _this$props.count;
        var message = messageData.message;
        var messageJson = _isEmpty(message) ? undefined : JSON.parse(message);

        if (!_isEmpty(messageJson)) {
          var tenantId = messageJson.tenantId,
              number = messageJson.number;
          var newCount = count;

          if (tenantId === 0) {
            newCount = Number(count) + Number(number);
          } else if (tenantId === getCurrentOrganizationId()) {
            newCount = Number(count) + Number(number);
          }

          saveNotices({
            count: newCount > 0 ? newCount : 0
          });

          if (Number(number) > 0) {
            var key = uuid();
            notification.info({
              key: key,
              icon: /*#__PURE__*/React.createElement(React.Fragment, null),
              message: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Icons, {
                type: "bell",
                size: 30,
                color: "red",
                style: {
                  position: 'relative',
                  top: 4,
                  right: 15
                }
              }), intl.get('cpaas.common.basicLayout.newMessage').d('您有新的消息')),
              description: /*#__PURE__*/React.createElement("a", {
                onClick: function onClick() {
                  _this.handleClick(key);
                },
                style: {
                  paddingLeft: 35
                }
              }, intl.get('cpaas.common.basicLayout.watchMessage').d('查看消息')),
              className: 'request info'
            });
          }
        }
      });
    }
  }, {
    key: "handleClick",
    value: function handleClick(key) {
      openTab({
        key: '/cmsg/user-message',
        path: '/cmsg/user-message/list',
        title: 'cpaas.common.title.userMessage',
        icon: undefined,
        closable: true,
        search: ''
      });
      Notification.close(key);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      webSocketManager.destroyWebSocket();
    }
  }, {
    key: "render",
    value: function render() {
      return null;
    }
  }]);

  return DefaultListenWebSocket;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "handleClick", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleClick"), _class.prototype)), _class));
export default connect(function (_ref) {
  var _ref$global = _ref.global,
      global = _ref$global === void 0 ? {} : _ref$global;
  return {
    count: global.count
  };
}, function (dispatch) {
  return {
    saveNotices: function saveNotices(payload) {
      return dispatch({
        type: 'global/saveNotices',
        payload: payload
      });
    }
  };
})(DefaultListenWebSocket);